package com.huika.dealontop.control.pay.view;

import android.graphics.Bitmap;
import android.net.http.SslError;
import android.os.Message;
import android.view.KeyEvent;
import android.webkit.HttpAuthHandler;
import android.webkit.SslErrorHandler;
import android.webkit.WebResourceResponse;
import android.webkit.WebView;
import android.webkit.WebViewClient;

/**
 * Author: Victor Wu <br/>
 * Email: victor-wu*foxmail.com <br/>
 * Time: 2015/12/31 11:09 <br/>
 * Description: WebViewClient 代理类
 */
public class WebViewClientWarp extends WebViewClient {

    private WebViewClient subclassClient ;

    public WebViewClientWarp(){}

    public WebViewClientWarp(WebViewClient subclassClient){
        this .subclassClient = subclassClient;
    }

    public WebViewClient getSubclassClient() {
        return subclassClient;
    }

    public void setSubclassClient(WebViewClient subclassClient) {
        this .subclassClient = subclassClient;
    }

    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (subclassClient != null){
            return subclassClient .shouldOverrideUrlLoading(view, url);
        }else {
            return super.shouldOverrideUrlLoading(view, url);
        }
    }

    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        subclassClient .onPageStarted(view, url, favicon);
    }

    public void onPageFinished(WebView view, String url) {
        if (subclassClient != null){
            subclassClient.onPageFinished(view, url);
        }else {
            super.onPageFinished(view, url);
        }
    }

    public void onLoadResource(WebView view, String url) {
        if (subclassClient != null){
            subclassClient.onLoadResource(view, url);
        }else {
            super.onLoadResource(view, url);
        }
    }

    public WebResourceResponse shouldInterceptRequest(WebView view,
                                                      String url) {
        if (subclassClient != null){
            return subclassClient .shouldInterceptRequest(view, url);
        }else {
            return super.shouldInterceptRequest(view, url);
        }
    }

    @Deprecated
    public void onTooManyRedirects(WebView view, Message cancelMsg,
                                   Message continueMsg) {

        if (subclassClient != null){
            subclassClient.onTooManyRedirects(view, cancelMsg, continueMsg);
        }else {
            super.onTooManyRedirects(view, cancelMsg, continueMsg);
        }
    }
    public void onReceivedError(WebView view, int errorCode,
                                String description, String failingUrl) {
        if (subclassClient != null){
            subclassClient.onReceivedError(view, errorCode, description, failingUrl);
        }else {
            super.onReceivedError(view, errorCode, description, failingUrl);
        }
    }

    public void onFormResubmission(WebView view, Message dontResend,
                                   Message resend) {
        if (subclassClient != null){
            subclassClient.onFormResubmission(view, dontResend, resend);
        }else {
            super.onFormResubmission(view, dontResend, resend);
        }
    }

    public void doUpdateVisitedHistory(WebView view, String url,
                                       boolean isReload) {
        if (subclassClient != null){
            subclassClient.doUpdateVisitedHistory(view, url, isReload);
        }else {
            super.doUpdateVisitedHistory(view, url, isReload);
        }
    }

    public void onReceivedSslError(WebView view, SslErrorHandler handler,
                                   SslError error) {
        if (subclassClient != null){
            subclassClient.onReceivedSslError(view, handler, error);
        }else {
            super.onReceivedSslError(view, handler, error);
        }
    }

    public void onReceivedHttpAuthRequest(WebView view,
                                          HttpAuthHandler handler, String host, String realm) {
        if (subclassClient != null){
            subclassClient.onReceivedHttpAuthRequest(view, handler, host, realm);
        }else {
            super.onReceivedHttpAuthRequest(view, handler, host, realm);
        }
    }

    public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {

        if (subclassClient != null){
            return subclassClient .shouldOverrideKeyEvent(view, event);
        }else {
            return super.shouldOverrideKeyEvent(view, event);
        }
    }

    public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
        if (subclassClient != null){
            subclassClient.onUnhandledKeyEvent(view, event);
        }else {
            super.onUnhandledKeyEvent(view, event);
        }
    }

    public void onScaleChanged(WebView view, float oldScale, float newScale) {

        if (subclassClient != null){
            subclassClient.onScaleChanged(view, oldScale, newScale);
        }else {
            super.onScaleChanged(view, oldScale, newScale);
        }
    }

    public void onReceivedLoginRequest(WebView view, String realm,
                                       String account, String args) {
        if (subclassClient != null){
            subclassClient.onReceivedLoginRequest(view, realm, account, args);
        }else {
            super.onReceivedLoginRequest(view, realm, account, args);
        }
    }
}

